package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.commen.Result;
import com.itheima.entiry.AddressBook;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    public Result add(HttpSession session, @RequestBody AddressBook addressBook){
        Long userId = (Long) session.getAttribute("user");
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return new Result(1);
    }

    @PutMapping("/default")
    public Result defaultAddress(@RequestBody AddressBook addressBook){
//        LambdaQueryWrapper<AddressBook> addQw = new LambdaQueryWrapper<>();
//        addQw.eq(AddressBook::getIsDefault,1);
//        List<AddressBook> addressBooks = addressBookService.list(addQw);
//        if (addressBooks.size()==0){
//
//        }
//        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
//        qw.set(AddressBook::getIsDefault,0);
//        qw.eq(AddressBook::getUserId,addressBook.getUserId());
//        addressBookService.update(qw);
//        Long id = addressBook.getId();
//        LambdaUpdateWrapper<AddressBook> qw2 = new LambdaUpdateWrapper<>();
//        qw2.set(AddressBook::getIsDefault,1);
//        qw2.eq(AddressBook::getId,id);
//        addressBookService.update(qw2);
        AddressBook byId = addressBookService.getById(addressBook.getId());
        Long userId = byId.getUserId();
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getUserId,userId);
        qw.set(AddressBook::getIsDefault,0);
        addressBookService.update(qw);
        LambdaUpdateWrapper<AddressBook> qw2 = new LambdaUpdateWrapper<>();
        qw2.eq(AddressBook::getId,addressBook.getId());
        qw2.set(AddressBook::getIsDefault,1);
        addressBookService.update(qw2);
        return new Result(1);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        AddressBook addressBookServiceById = addressBookService.getById(id);
        if (addressBookServiceById==null){
            return new Result(0);
        }
        return new Result(1,addressBookServiceById);
    }

    @GetMapping("/default")
    public Result getDefault(HttpSession session){
        Long user = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getIsDefault,1);
        qw.eq(AddressBook::getUserId,user);
        AddressBook addressBook= addressBookService.getOne(qw);
        return new Result(1,addressBook);
    }

    @GetMapping("/list")
    public Result getAll(HttpSession session){
        Long user = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,user);
        List<AddressBook> list = addressBookService.list(qw);
        return new Result(1,list);
    }

    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.in(AddressBook::getId,ids);
        addressBookService.remove(qw);
        return new Result(1);
    }

    @PutMapping
    public Result change(@RequestBody AddressBook addressBook){
        Long id = addressBook.getId();
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getId,id);
        addressBookService.update(addressBook,qw);
        return new Result(1);
    }
}
